const PluginName = 'MpProjectPlugin'

class MpProjectPlugin {
  apply(compiler) {
    compiler.hooks.compilation.tap(PluginName, compilation => {
      compilation.hooks.buildModule.tap(PluginName, module => {
        try {
          const matches = module.resource.match(new RegExp(`${compiler.context}/src/([^/\.]*)\.js`))
          if (matches) {
            const content = fs.readFileSync(module.resource, 'utf-8');
            global.tabbarPageMap[matches[1]] = /\/\/ *@ *tab-bar-page */.test(content);
            global.showTabbarMap[matches[1]] = /\/\/ *@ *show-tab-bar */.test(content);
          }
        } catch (e) {
          // console.error(e);
        }
      })
  }
}

module.exports = MpProjectPlugin
